package com.hukun.ssm1110.controller;


import com.hukun.ssm1110.entity.User;
import com.hukun.ssm1110.mapper.PasswordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.support.AbstractMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PasswordController {


    @RequestMapping("/password")
    public String passwordPage() {
        return "password";
    }

    @Autowired
    PasswordMapper passwordMapper;

    @ResponseBody //返回的是 对象而不是页面 返回到当前页面
    @RequestMapping("/checkPassword")
    public String checkPassword(String password, HttpServletRequest request) {
        HttpSession session = request.getSession();

        User LoginUser = (User) session.getAttribute("loginUser");
        User user = new User();
        user.setId(LoginUser.getId());
        user.setPassword(password);
        String s = passwordMapper.checkPassword(user);
        return s;
    }

    /*
    修改密码逻辑
     */

    @RequestMapping("updatePassword")
    public String updatePassword(User user,HttpServletRequest request) {
        HttpSession session = request.getSession();
        User LoginUser = (User) session.getAttribute("loginUser");
        user.setId(LoginUser.getId());
        int i = passwordMapper.updatePassword(user);
        if(i>0)
        {
            return "login";
        }
        else {
            return "password";
        }

    }


}
